package com.it.service.impl;

import com.it.mapper.MenuMapper;
import com.it.mapper.MenuRoleMapper;
import com.it.pojo.Menu;
import com.it.pojo.MenuExample;
import com.it.service.MenuService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.management.MemoryUsage;
import java.time.temporal.ValueRange;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/14 12:00
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;


    /*查询一级彩打*/
    @Override
    public List<Menu> findByUserId(String userId) {
        return menuMapper.findByUserId(userId);
    }

    /*查询二级菜单*/
    @Override
    public List<Menu> findByParentId(List<String> ids) {
        return menuMapper.findByParentId(ids);
    }

    /*已拥有菜单*/
    @Override
    public List<Menu> findByRoleId(String roleId) {
        return menuMapper.findByRoleId(roleId);
    }

    @Override
    public List<Menu> findByNoRoleId(String roleId) {
        /*已拥有菜单*/
        List<Menu> list1 = findByRoleId(roleId);
        /*所有菜单*/
        List<Menu> list = findAll();
        /*未拥有菜单*/
        List<Menu> collect = list.stream().filter(o1 -> !list1.stream().anyMatch(o2 -> o1.getMenuId().equals(o2.getMenuId()))).collect(Collectors.toList());
        return collect;
    }

    /*所有菜单*/
    @Override
    public List<Menu> findAll() {
        MenuExample example = new MenuExample();
        MenuExample.Criteria criteria = example.createCriteria();
        criteria.andMenuLevelidEqualTo(1);
        criteria.andMenuDelEqualTo(0);
        return menuMapper.selectByExample(example);
    }

    @Override
    public List<Menu> findAllMenuList2(String menuId) {
        MenuExample example = new MenuExample();
        MenuExample.Criteria criteria = example.createCriteria();
        criteria.andMenuParentidEqualTo(Integer.parseInt(menuId));
        criteria.andMenuDelEqualTo(0);
        return menuMapper.selectByExample(example);
    }

    /*根据menuId删除一级菜单*/
    @Override
    public int deleteByMenuId(String menuId) {
        Menu menu = menuMapper.selectByPrimaryKey(menuId);
        menu.setMenuDel(1);
        return menuMapper.updateByPrimaryKeySelective(menu);
    }

    @Override
    public Menu findById(String menuId) {
        return menuMapper.selectByPrimaryKey(menuId);
    }

    @Override
    public int addMenuLevelOne(Menu menu) {
        if (!StringUtils.isNullOrEmpty(menu.getMenuId())){
            return menuMapper.updateByPrimaryKeySelective(menu);
        }else{
            menu.setMenuLevelid(1);
            menu.setMenuId(getPrimaryKey());
            return menuMapper.insertSelective(menu);
        }
    }

    public String getPrimaryKey(){
        MenuExample example = new MenuExample();
        List<Menu> list = menuMapper.selectByExample(example);
        return list.size()+1+"";
    }


}
